﻿using System.Collections.Generic;
using UnityEngine;
using FairyGUI;
using System;

public class FSuitListMediator : MyWindow {
    GList _list;
    FEditSuitMediator _editSuitPanel;
    GComponent _suitList;
    GTextField _suitNum;

    static List<Suit> _suitDataList;//所有装备数据
    static List<Suit> _curSuitDataList;//筛选后的装备数据
    static FSuitListMediator _instance;

    OneDelegate _callback;

    protected override void OnInit(){
        base.OnInit();
        this.contentPane = UIPackage.CreateObject("EquipmentPackage", "SuitList").asCom;
        _suitList = this.contentPane;
        InitPanel();
    }

    public FSuitListMediator(OneDelegate cb = default(OneDelegate))
    {
        _instance = this;

        SetCallBack(cb);
    }

    public void SetCallBack(OneDelegate cb)
    {
        _callback = cb;
        FreshCallBackPanel();
    }

    private void FreshCallBackPanel()
    {
        if (_suitList == null) return;
        var closeBtn = _suitList.GetChild("closeBtn");
        if (_callback != null)
        {
            closeBtn.onClick.Add(OnCloseBtn);
            closeBtn.visible = true;
        }
        else
        {
            closeBtn.visible = false;
        }
    }

    private void InitPanel()
    {
        FreshCallBackPanel();
        _suitNum = _suitList.GetChild("numText").asTextField;
        _suitList.GetChild("newBtn").onClick.Add(OnNewBtnClick);
        _list = _suitList.GetChild("suitList").asList;
        //编辑装备面板
        _editSuitPanel = (FEditSuitMediator)FUIMain.GetWindow(GMConfig.PanelType.EditSuitPanel,null);

        InitData();
    }
    private static void InitData()
    {
        if (_suitDataList != null) return;
        _suitDataList = SuitMO.GetSuitList();
    }
	// Use this for initialization
    protected override void OnShown()
    {         
        //设置item单机事件监听
        _list.onClickItem.Add(OnSuitListClick);
        _list.itemRenderer = RenderListItem;

        FreshSuitList();
    }

    void RenderListItem(int index, GObject item)
    {
        var btn = (FSuit)item;
        btn.InitData(_curSuitDataList[index]);
    }

    private void OnSuitListClick(EventContext context)
    {
        var index = _list.GetChildIndex((GObject)context.data);
        if(_callback != null)
        {
            _callback(_curSuitDataList[index].id);
        }
        else
        {
            _editSuitPanel.Show();
            //获取对应所有装备内的下标，以便后面保存
            var _index = SuitMO.GetSuitIndex(_curSuitDataList[index]);
            _editSuitPanel.InitData(_curSuitDataList[index], _index);
        }
    }
    /// <summary>
    /// 新建装备
    /// </summary>
    private void OnNewBtnClick()
    {
        //创建一张新卡
        var _suit = new Suit();
        int lastId = 0;
        if (_suitDataList.Count != 0)
        {
            var lastCard = _suitDataList[_suitDataList.Count - 1];
            lastId = System.Convert.ToInt32(lastCard.id.Split('_')[1]);
        }
        _suit.id = "Suit_" + (lastId + 1);
        SuitMO.ResetSuitName(_suit);
        _editSuitPanel.Show();
        _editSuitPanel.InitData(_suit,_suitDataList.Count);
    }
    /// <summary>
    /// 保存装备
    /// </summary>
    /// <param name="suitData"></param>
    /// <param name="index"></param>
    /// <returns></returns>
    public static bool SaveSuit(Suit suitData,int index)
    {
        SuitMO.SaveSuit(suitData,index);
        if(_instance != null) _instance.FreshSuitList();
        return true;
    }
    /// <summary>
    /// 删除装备
    /// </summary>
    /// <param name="index"></param>
    /// <returns></returns>
    public static bool DelSuit(int index){
        if (index < 0 || index >= _suitDataList.Count) return false;
        SuitMO.DelSuit(index);
        if(_instance != null) _instance.FreshSuitList();
        return true;
    }
    /// <summary>
    /// 刷新装备面板
    /// </summary>
    private void FreshSuitList(){
        _curSuitDataList = _suitDataList;
        // _list.RemoveChildrenToPool();
        _list.numItems = _curSuitDataList.Count;
        _suitNum.text = _curSuitDataList.Count.ToString();
    }
	// Update is called once per frame
	void Update () {
		
	}
    /// <summary>
    /// 关闭面板
    /// </summary>
    private void OnCloseBtn()
    {
        if (_callback != null) _callback(null);
    }
}
